package com.mcxtzhang.indexlib.IndexBar.bean;

import com.github.promeg.pinyinhelper.Pinyin;
import com.mcxtzhang.indexlib.suspension.ISuspensionInterface;

/**
 * 介绍：索引类的汉语拼音的接口
 * 作者：zhangxutong
 * 邮箱：mcxtzhang@163.com
 * CSDN：http://blog.csdn.net/zxt0601
 * 时间： 16/09/04.
 */

public abstract class BaseIndexPinyinBean implements ISuspensionInterface
{
    /**
     * 拼音
     */
    protected String baseIndexPinyin;

    public String getBaseIndexPinyin()
    {
        return baseIndexPinyin;
    }

    public BaseIndexPinyinBean setBaseIndexPinyin(String baseIndexPinyin)
    {
        this.baseIndexPinyin = baseIndexPinyin;
        return this;
    }

    @Override
    public boolean isShowSuspension()
    {
        // 不能排序的不显示分组标题
        boolean unsort = !getIndexTag().equals(unletterTag());
        return unsort;
    }

    @Override
    public String getIndexTag()
    {
        if (null == sortContent() || "".equals(sortContent().trim()))
            return unletterTag();

        String sort = sortContent().substring(0, 1);

        if (!sort.matches("^[A-Z]"))
            return unletterTag();

        return sort;
    }

    @Override
    public String sortContent()
    {
        if (null != baseIndexPinyin)
            return baseIndexPinyin;

        String content = showContent();// 对显示内容取拼音
        if (null == content || "".equals(content.trim()))
            baseIndexPinyin = "";
        else
        {
            baseIndexPinyin = Pinyin.toPinyin(content.charAt(0));
            if (null == baseIndexPinyin || "".equals(baseIndexPinyin.trim()))
                baseIndexPinyin = "";
            else
                baseIndexPinyin = baseIndexPinyin.trim().toUpperCase();// 这里转大写
        }

        return baseIndexPinyin;
    }

    @Override
    public String getSuspensionTag()
    {
        return getIndexTag();
    }

    /**
     * 当字符不是英文拼音时，使用何种排序依据
     *
     * @return
     */
    protected String unletterTag()
    {
        return "";
    }

    /**
     * 显示内容
     *
     * @return
     */
    protected abstract String showContent();

    @Override
    public int fixAlign()
    {
        return ISuspensionInterface.SORT;
    }
}
